package com.liveneo.adapter.mobile.web.controller;

import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.request.LoginRequest;
import com.liveneo.adapter.mobile.model.request.RegisterUserRequest;
import com.liveneo.adapter.mobile.model.request.SurveyUserCodeRequest;
import com.liveneo.adapter.mobile.model.request.UpdatePwdRequest;
import com.liveneo.adapter.mobile.model.response.LoginResponse;
import com.liveneo.adapter.mobile.service.MobileAuthService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.AppException;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * 移动端用户操作接口
 * 
 * @author wangxx
 * 
 * @since 2016-06-01
 */
@Controller
@RequestMapping("/mobile")
public class MobileAuthController extends BaseController {
    @Autowired
    private MobileAuthService mobileAuthService;

    @RequestMapping("login.action")
    @ResponseBody
    public String login(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "MP01入参=" + logUUid;
        LoginRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LoginRequest.class);
        LoginResponse response = null;
        String result = "";
        try {
            response = mobileAuthService.login(request);
            response.setToken(UUID.randomUUID().toString().replace("-", ""));
        } catch (Exception e) {
            LogUtils.error("MP01移动端登陆时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LoginResponse.class);
        } finally {
            result = decorateResult("MP01出参=" + logUUid, response);
        }
        return result;
    }

    @RequestMapping("updatePassword.action")
    @ResponseBody
    public String updatePassword(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "MP02入参=" + logUUid;
        UpdatePwdRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, UpdatePwdRequest.class);
        BaseResponse baseResponse = null;
        String result = "";
        try {
            String surveyUserCode = request.getUserCode();
            String oldPassword = request.getOldPassWord();
            String newPassword = request.getNewPassWord();
            baseResponse = mobileAuthService.updatePassword(surveyUserCode, oldPassword, newPassword);
        } catch (Exception e) {
            LogUtils.error("MP02移动端更改密码出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult("MP02出参=" + logUUid, baseResponse);
        }
        return result;
    }

    /**
     * MP03
     * @author Liuzhipeng 2016年11月4日 下午1:27:43
     * @param data
     * @return
     */
    @RequestMapping("registerUser.action")
    @ResponseBody
    public String registerUser(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "MP03入参=" + logUUid;
        RegisterUserRequest requester = JsonUtils.fromJsonWithBase64(logTitle, data, RegisterUserRequest.class);
        BaseResponse baseResponse = null;
        String result = "";
        try {
            String phoneNo = requester.getPhoneNo();
            String handleType = requester.getHandleType();
            if (phoneNo == null || "".equals(phoneNo)) {
                baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_ERR_PARAM_NO_EXISTS);
            } else {
                if ("1".equals(handleType)) {
                    mobileAuthService.registerSuccess(requester);
                    baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
                } else if ("2".equals(handleType)) {
                    mobileAuthService.cancleSuccess(requester);
                    baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
                } else {
                    baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_ERR_PARAM, MobileReturnCodeSupport.SYS_CODE,
                            MobileReturnCodeSupport.CODE_ERR_PARAM_NO_EXISTS);
                }
            }
        } catch (Exception e) {
            LogUtils.error("MP03移动端注册时出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult("MP03出参=" + logUUid, baseResponse);
        }
        return result;
    }

    @RequestMapping("loginOut.action")
    @ResponseBody
    public String logout(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "MP30入参=" + logUUid;
        SurveyUserCodeRequest requester = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyUserCodeRequest.class);
        BaseResponse baseResponse = null;
        String result = "";
        try {
            String userCode = requester.getUserCode();
            mobileAuthService.logout(userCode);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("MP30登出用户时出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("MP30登出用户时出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult("MP30出参=" + logUUid, baseResponse);
        }
        return result;
    }

    @RequestMapping("userOnline.action")
    @ResponseBody
    public String userOnline(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "MP19入参=" + logUUid;
        SurveyUserCodeRequest requester = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyUserCodeRequest.class);
        BaseResponse baseResponse = null;
        String result = "";
        try {
            String userCode = requester.getUserCode();
            mobileAuthService.userOnline(userCode);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("MP19更新客户端在线时间出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("MP19更新客户端在线时间出错，错误信息:", e);
            baseResponse = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = decorateResult("MP19出参=" + logUUid, baseResponse);
        }
        return result;
    }
}
